/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
var EventProxy = require('eventproxy');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');


var pfLogController = {
    getPfLogPage: function (req, res) {
        co(function () {
            var params = {};
            var paramT = {};
            var page = parseInt(req.query.page);
            var pfLogSql = "select pfLog.create_time,meet.title as meetTitle,question.title as queTitle,chek.title as cheTitle,judMem.name as judName,judMem.tel as judTel,judMem.hospital_name as judHospitalName,playMem.name as playName,playMem.tel as playTel,playMem.hospital_name as playHospitalName,pfLog.score from pf_log as pfLog " +
                " left join pf_meet as meet on pfLog.meet_num = meet.meet_num left join pf_question as question on pfLog.question_id = question.id left join pf_check as chek on pfLog.check_id = chek.id " +
                " left join pf_member as judMem on pfLog.judge_open_id = judMem.open_id left join pf_member as playMem on pfLog.player_id = playMem.id where 1=1 ";
            if (req.query.meetNum != undefined) {
                pfLogSql += " and pfLog.meet_num = '" + req.query.meetNum + "'";
                params.meetNum = req.query.meetNum;
            }
            if (req.query.judgeOpenId != undefined) {
                pfLogSql += " and pfLog.judge_open_id = '" + req.query.judgeOpenId + "'";
                params.judgeOpenId = req.query.judgeOpenId;
            }
            if (req.query.playerId != undefined) {
                pfLogSql += " and pfLog.player_id = '" + req.query.playerId + "'";
                params.playerId = req.query.playerId;
            }
            pfLogSql += " order by pfLog.create_time DESC ";
            if (req.query.page != undefined && req.query.rows != undefined) {
                pfLogSql += " limit " + parseInt(req.query.page) + "," + parseInt(req.query.rows);
            }
            paramT.where = params;
            var ep = new EventProxy();

            seq.sequelize.query(pfLogSql).spread(function (result) {
                ep.emit('result', result);
            }).catch(function (err) {
                res.json({ 'success': false, 'msg': '查询数据失败' });
            });

            seq.pfLogModule.count(paramT).then(function (count) {
                ep.emit('total', count);
            }).catch(function (err) {
                res.json({ 'success': false, 'msg': '查询总数失败' });
            });
            ep.all('result', 'total', function (result, total) {
                res.json({ 'success': true, 'msg': '查询成功！', 'page': page, 'total': total, 'result': result });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })
    },
    resultScore: function (req, res, next) {
        co(function () {
            var player = req.params.playerId;
            var paramT = {};
            var params = {};
            if (req.query.meetNum != undefined) {
                params.meetNum = req.query.meetNum;
            }
            params.playerId = player;
            paramT.where = params;
            paramT.group = [["judge_open_id"]];
            var ep = new EventProxy();
            seq.pfLogModule.findAll(paramT).then(function (result) {
                ep.emit('count', result.length);
            })
            seq.pfLogModule.sum('score', { where: { "playerId": player } }).then(function (total) {
                ep.emit('total', total);
            })
            ep.all('count', 'total', function (count, total) {
                res.json({ 'success': true, 'msg': '查询成功！', 'result': total / count });
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(e);
        })
    }
}
module.exports = pfLogController;